FROM dk9d7tznczn4xf.xuanyuan.run/ubuntu:24.04

LABEL maintainer="OHOS Test Server"
LABEL description="OpenHarmony 测试服务器集合 - DNS, MQTT, Socket.IO, HTTP/HTTPS"
LABEL version="2.0.0"

ARG BUILD_DATE
ARG VERSION

ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Asia/Shanghai

# 安装基础依赖
RUN apt update && \
    apt install -y \
        curl \
        wget \
        git \
        tinyproxy \
        openssl \
        ca-certificates \
        tzdata && \
    ln -fs /usr/share/zoneinfo/${TZ} /etc/localtime && \
    dpkg-reconfigure -f noninteractive tzdata && \
    apt clean && \
    rm -rf /var/lib/apt/lists/*

# 安装 Golang（使用国内镜像源）
ENV GOLANG_VERSION=1.22.4
RUN set -e && \
    echo "正在下载 Go ${GOLANG_VERSION}..." && \
    # 尝试多个下载源
    (wget --timeout=30 --tries=3 -q -O go.tar.gz https://golang.google.cn/dl/go${GOLANG_VERSION}.linux-amd64.tar.gz || \
     wget --timeout=30 --tries=3 -q -O go.tar.gz https://mirrors.aliyun.com/golang/go${GOLANG_VERSION}.linux-amd64.tar.gz || \
     wget --timeout=30 --tries=3 -q -O go.tar.gz https://go.dev/dl/go${GOLANG_VERSION}.linux-amd64.tar.gz) && \
    echo "解压 Go..." && \
    tar -C /usr/local -xzf go.tar.gz && \
    rm go.tar.gz && \
    echo "Go 安装完成"

# 设置 Go 环境变量
ENV PATH=/usr/local/go/bin:${PATH}
ENV GOPROXY=https://goproxy.cn,direct
ENV GO111MODULE=on

# 安装 Node.js 和 npm（使用国内镜像源）
RUN set -e && \
    echo "正在安装 Node.js..." && \
    # 尝试多个源
    (curl --connect-timeout 30 --max-time 60 -fsSL https://deb.nodesource.com/setup_20.x | bash - || \
     curl --connect-timeout 30 --max-time 60 -fsSL https://mirrors.tuna.tsinghua.edu.cn/nodesource/deb_20.x/setup_20.x | bash -) && \
    apt-get install -y nodejs && \
    echo "配置 npm 镜像..." && \
    npm config set registry https://registry.npmmirror.com && \
    npm install -g http-server && \
    apt clean && \
    rm -rf /var/lib/apt/lists/*

# 验证安装
RUN echo "=== 验证环境 ===" && \
    go version && \
    node --version && \
    npm --version && \
    openssl version

# 设置工作目录
WORKDIR /app

# 复制项目文件
COPY config.json ./
COPY manage_servers.sh ./
COPY generate_test_certs.sh ./
COPY tinyproxy.conf /etc/tinyproxy/tinyproxy.conf
COPY server/ ./server/

# 创建必要的目录
RUN mkdir -p logs pids cert && \
    chmod +x manage_servers.sh generate_test_certs.sh

# 安装 Node.js 依赖
RUN cd server/ohos_dns_server && npm install --production && \
    cd ../ohos_socketio_tls_server && npm install --production && \
    cd ../ohos_axios_server && npm install --production

# 安装 Go 依赖
RUN cd server/ohos_mqtt_server && \
    go mod download && \
    go mod verify

# 暴露所有服务端口
EXPOSE 53/udp 1882 1883 3100 3101 4000 4001 4002 4003 4004 4005 4006 8005 8080

# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
    CMD curl -f http://localhost:4000/ || exit 1

# 创建启动脚本
RUN echo '#!/bin/bash\n\
set -e\n\
echo "==========================================="\n\
echo "  OHOS 测试服务器容器启动中..."\n\
echo "==========================================="\n\
echo ""\n\
# 生成测试证书\n\
echo "📝 生成测试证书..."\n\
if [ ! -f "cert/server.crt" ]; then\n\
    mkdir -p cert\n\
    cd cert\n\
    openssl req -x509 -newkey rsa:2048 -nodes \\\n        -keyout server.key -out server.crt -days 365 \\\n        -subj "/C=CN/ST=Test/L=Test/O=OHOS/CN=localhost" \\\n        -addext "subjectAltName=DNS:localhost,IP:127.0.0.1,IP:0.0.0.0" 2>/dev/null\n\
    echo "✅ 证书生成完成"\n\
    cd ..\n\
else\n\
    echo "✅ 证书已存在"\n\
fi\n\
echo ""\n\
# 启动所有服务器\n\
echo "🚀 启动所有测试服务器..."\n\
echo ""\n\
./manage_servers.sh start all\n\
echo ""\n\
echo "==========================================="\n\
echo "  所有服务器已启动"\n\
echo "==========================================="\n\
echo ""\n\
echo "📋 服务器访问信息:"\n\
echo "  - DNS Server: 53 (UDP)"\n\
echo "  - MQTT TCP: 1883"\n\
echo "  - MQTT WebSocket: 1882"\n\
echo "  - MQTT Stats: 8080"\n\
echo "  - Socket.IO HTTP: 3100"\n\
echo "  - Socket.IO HTTPS: 3101"\n\
echo "  - HTTP Server: 4000-4005"\n\
echo "  - Static Server: 4006"\n\
echo "  - Proxy Server: 8005"\n\
echo ""\n\
# 保持容器运行并显示日志\n\
tail -f /dev/null\n\
' > /app/docker-entrypoint.sh && \
    chmod +x /app/docker-entrypoint.sh

# 设置入口点
ENTRYPOINT ["/app/docker-entrypoint.sh"]

# 元数据
LABEL build_date="${BUILD_DATE}"
LABEL version="${VERSION}"